package com.dingup.paike.service.pk.impl;

import com.dingup.api.adapter.classInfo.ClassInfoAdapter;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.model.classInfo.ClassUserInfo;
import com.dingup.paike.feign.client.classInfo.ClassInfoFeignClient;
import com.dingup.paike.feign.client.user.UserFeignClient;
import com.dingup.paike.service.pk.ClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/11/29.
 */
@Service
public class ClassInfoServiceImpl implements ClassInfoService {

    @Autowired
    ClassInfoFeignClient classInfoFeignClient;
    @Autowired
    UserFeignClient userFeignClient;

    @Override
    public List<SelectBean> getClassTeacher(Integer classId) {
        List<SelectBean> users = userFeignClient.getAllUserName();
        ClassInfoAdapter classInfoAdapter = classInfoFeignClient.getClassInfoById(classId);
        if (classInfoAdapter == null) {
            return users;
        }
        List<ClassUserInfo> teacher = classInfoAdapter.getTeachers();
        List<String> teacherIds = teacher.parallelStream().map(ClassUserInfo::getUserId).collect(Collectors.toList());
        List<SelectBean> classTeacher = users.parallelStream().filter(s -> teacherIds.contains(s.getValue())).collect(Collectors.toList());
        List<SelectBean> other = users.parallelStream().filter(s -> !teacherIds.contains(s.getValue())).collect(Collectors.toList());
        classTeacher.addAll(other);
        return classTeacher;
    }
}
